<template>
  <div>
    <!--部门数据-->
      <div class="head-container">
        <el-input
          v-model="deptName"
          placeholder="请输入部门名称"
          clearable
          size="small"
          prefix-icon="el-icon-search"
          style="margin-bottom: 20px"
        />
      </div>
      <div class="head-container">
        <el-tree
          :data="deptOptions"
          :props="defaultProps"
          :expand-on-click-node="false"
          :filter-node-method="filterNode"
          ref="tree"
          node-key="id"
          default-expand-all
          highlight-current
          show-checkbox
          @check="handleNodeClick"
        />
      </div>
  </div>
</template>

<script>
import {  deptTree4workflow} from '@/api/flowable/definition'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {StrUtil} from '@/utils/StrUtil'

export default {
  name: "FlowDept",
  components: { Treeselect },
  // 接受父组件的值
  props: {
    // 回显数据传值
    selectData: {
      type: String,
      default: null,
      required: false
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 部门名称
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 部门树选项
      deptOptions: undefined,
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getDeptTree();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTree4workflow().then(response => {
        this.deptOptions = response.data;
        this.$nextTick(() => {
          // 打开时回显数据
          this.echoChooseItems();
        });
      });
    },
    // 回显（勾选）已选事项
    echoChooseItems() {
      if (StrUtil.isNotBlank(this.selectData)){
        if (this.deptOptions.length > 0) {
          // 通过 keys 设置目前勾选的节点，使用此方法必须设置 node-key 属性
          this.$refs.tree.setCheckedKeys(this.selectData.split(","));
        }
      }
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data,node) {
      this.$emit('handleAssignSelect', node.checkedNodes);
    },
  }
};
</script>
<style>
/*隐藏radio展示的label及本身自带的样式*/
/*.el-radio__label{*/
/*  display:none;*/
/*}*/
</style>
